<?php
/**
 * 请假申请列表job
 * 1.出差一个特定的部门审批人
 * 2.其他假不大于三天的一级领导审批即可
 * 3.其他假期超过三天的需二级领导审批
 * 4.其他假若二级领导是bos，则一级领导审批通过即可
 * 5.已经审批的记录要放在审批里面（包括撤销）
 *
 */
apf_require_class('Oa_Job_Attendance_Abstract');
class Oa_Job_Attendance_AuditList extends Oa_Job_Attendance_Abstract {
    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    private $trip_manager_info;

    private $update_time = 15;//更新时间分钟

    public function run($params){
        $this->log("开始查询...");
        //定时
        if ($params) {
            $this->update_time = $params;

            $records = $this->get_leave_record_by_time($this->update_time); //获取特定时间段内的请假记录

            $filtered_records = $this->filter_records($records); //分类处理

            $trip_audit = $this->process_trip($filtered_records['trip']); //出差处理逻辑

            $att_audit = $this->process_att($filtered_records['att']);//其他

            $audit = array_merge($trip_audit,$att_audit);

            $this->replace_into_audit($audit);

        //历史数据处理逻辑
        } else {
            $num = 100;
            do {
                $records = $this->get_leave_record_by_offset($limit, $offset);
            } while(count($records));
        }

        $this->log("更新完毕");
    }


    /**
     * 分批取数据
     */
    public function get_leave_record_by_offset() {
    }

    public function replace_into_audit($array){
        $i = 0;
        foreach($array as $a){
            if($a['uid'] == NULL){
                continue;
            }
            $sql = "REPLACE INTO audit_list VALUES({$a['uid']},{$a['leave_id']},{$a['status']})";
            $this->execute($sql,array(),$this->writedb);
            echo $i.PHP_EOL;
            ++$i;
        }
    }

    /**
     * 过滤数据
     */
    public function filter_records($records) {
        $result = array();
        foreach ($records as $record) {
            if ($record['leave_reason'] == '地铁故障') { //地铁故障过滤处理
                unset($record);
            }
            if ($record['type'] == '出差') {
                $result['trip'][] = $record;
            } else {
                $result['att'][] = $record;
            }
        }
        return $result;
    }

    /**
     * 处理出差数据
     */
    public function process_trip($records){
        if(empty($records)){
            return array();
        }
        $this->trip_manager_info = $this->get_trip_manager_info();
        $trip_info = $this->trip_manager_info;
        $result = array();
        //特殊处理人员
        $email_list = array(
                'michaellv@anjukeinc.com',
                'garyhu@anjukeinc.com',
                'julianwang@anjukeinc.com',
                'justin@anjukeinc.com',
                'joychen@anjukeinc.com',
                'echoyang@anjukeinc.com',
                'vincentchang@anjukeinc.com',
                'henrytang@anjukeinc.com'
        );
        foreach ($records as $r){
            $uid = $trip_info[$r['department_id']]['user_id'];

            //特殊处理-------------------------
            if(in_array($r['email'],$email_list)){
                $uid = $this->get_boss_id();//审批者为mike
            }
            //-------------------------------

            //审批user_id不存在
            if($uid == NULL){
                continue;
            }
            //查看是否审批过，审批过在撤销了也要放在《已审批》里面，否则抛弃这条数据
            if($r['status'] == '未审核'){
                $status = 1;//表示未审核的
            }
            else{
                $record_log = $this->get_record_log($r['id']);
                if(empty($record_log)){
                    //没有审批前就撤销了，抛弃这条数据
                    continue;
                }
                else{
                    //表示已经审核过了，不管是批准还是驳回，状态未2
                    $status = 2;
                }
            }
            $result[] = array('uid'=>$uid,'leave_id'=>$r['id'],'status'=>$status);
        }
        return $result;
    }

    /**
     * 处理除出差外的数据
     */
    public function process_att($records){
        if(empty($records)){
            return array();
        }
        $result = array();
        foreach($records as $r){
            //没有一级领导的请假记录，直接处理掉
            $uid = $r['r_user_id'];
            if($uid ==NULL){
                continue;
            }
            //大于三天的处理
            if($r['leave_time'] > 3){
                $array = $this->extend_process($r);//判断返回的数组是否有两条数据
                if(empty($array)){
                    continue;
                }
                else{
                    foreach ($array as $a){//可能有两条记录
                        $result[] = $a;
                    }
                }
            }
            //不大于三天的数据处理
            else{
                //查看是否审批过，审批过在撤销了也要放在《已审批》里面，否则抛弃这条数据
                if($r['status'] == '未审核'){
                    $status = 1;//表示未审核的
                }
                else{
                    $record_log = $this->get_record_log($r['id']);
                    if(empty($record_log)){
                        //没有审批前就撤销了，抛弃这条数据
                        continue;
                    }
                    else{
                        //表示已经审核过了，不管是批准还是驳回，状态未2
                        $status = 2;
                    }
                }
                $result[] = array('uid'=>$uid,'leave_id'=>$r['id'],'status'=>$status);
            }
        }
        return $result;
    }

    /**
     * 三天以上的数据查看一级领导是否审批，顶级领导是否未bos
     */
    public function extend_process($r){
        $array = array();
        //查看二级领导是否是bos
        $bos_id = $this->get_boss_id();
        $uid = $r['r_user_id'];//一级领导
        if($uid == NULL){
            return $array;
        }
        $leave_id = $r['id'];
        //只有根据record_log和status状态来判断
        $record_log = $this->get_record_log($leave_id);
        if($r['status'] == '未审核'){
            //如果记录为0的话，返回一条记录，如果记录为1的话，表示一级一级审批已经通过了，同时要增加二级领导未批准的状态，返回二级领导未批准记录即可
            if(empty($record_log)){//返回第一级别未审批即可
                $status = 1;
                $array['r_user_id'] = array('uid'=>$uid,'leave_id'=>$leave_id,'status'=>$status);
            }
            else{
                $status = 2;//表示一级已经审批通过了，要先显示二级审批了
                $array['r_user_id'] = array('uid'=>$uid,'leave_id'=>$leave_id,'status'=>$status);
                $uid = $r['b_user_id'];
                $status = 1;
                if($r['b_user_id'] != $bos_id){
                    $array['b_user_id'] = array('uid'=>$uid,'leave_id'=>$leave_id,'status'=>$status);
                }
            }
        }
        else {
            if(empty($record_log)){
                //表示未审批前就已经撤销了
                return $array;//返回空数组
            }
            elseif(count($record_log) == 1){
                //表示一级已经驳回了,还是放在已审批里面
                $status = 2;
                $array['r_user_id'] = array('uid'=>$uid,'leave_id'=>$leave_id,'status'=>$status);
            }
            else{
                //有两条记录，肯定是都批过了，状态都为2
                $status = 2;//表示一级已经审批通过了，要先显示二级审批了
                $array['r_user_id'] = array('uid'=>$uid,'leave_id'=>$leave_id,'status'=>$status);
                $uid = $r['b_user_id'];
                $status = 2;
                if($r['b_user_id'] != $bos_id){
                    $array['b_user_id'] = array('uid'=>$uid,'leave_id'=>$leave_id,'status'=>$status);
                }
            }
        }
        return $array;
    }

    /**
     * 获取特定时间段的记录
     * 分钟
     */
    public function get_leave_record_by_time($time=0){
        $result = array();
        $time = (int)$time;
        $time = time()-$time*60;
        $date = date("Y-m-d H:i:s",$time);
        $sql = "select r.*,u.name,u.department_id,u.email from leave_record r,user_info u where r.user_id = u.user_id and  r.last_touch >= '{$date}'";
        $result = $this->get_db_rows($sql, $this->readdb);
        return $result;
    }

    /**
     * 获取record_log信息
     */
    public function get_record_log($leave_id){
        $sql = "select * from leave_record_log where leave_id = $leave_id ";
        $result = $this->get_db_rows($sql, $this->readdb);
        return $result;
    }

    /**
     * 获取所有出差管理信息
     */
    public function get_trip_manager_info(){
        $sql = "select * from trip_manager";
        $result = $this->get_db_rows($sql, $this->readdb);
        $date = array();
        foreach($result as $r){
            $date[$r['department_id']] = $r;
        }
        return $date;
    }


    private function get_boss_id(){
        //TODO 可配置code
        $code = "SHA001";
        $sql = "select * from user_info where code = '$code' ";
        $row = $this->get_db_row($sql, $this->readdb);
        $this->boss_id = $row['user_id'];
        return $row['user_id'];
    }
}